﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SEO.Model;
using System.Data.SQLite;

namespace SEO.DAL
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 关键词日分析表
    /// 完成日期：2011年6月7日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class KeywordsDayDAL
    {

        /// <summary>
        /// 获取KeywordsDay表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<KeywordsDay> GetAllKeywordsDayDAL()
        {
            List<KeywordsDay> list = new List<KeywordsDay>();
            string strSql = "select * from KeywordsDay";
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    KeywordsDay ky = new KeywordsDay();
                    ky.DayID = int.Parse(dr["day_id"].ToString());
                    ky.KeyName = dr["key_name"].ToString();
                    ky.WebUrl = dr["web_url"].ToString();
                    ky.CachedDate = dr["cached_date"].ToString();
                    list.Add(ky);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定KeywordsDay -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取KeywordsDay表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<KeywordsDay> GetKeywordsDayByWhereDAL(string strSql)
        {
            List<KeywordsDay> list = new List<KeywordsDay>();
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    KeywordsDay ky = new KeywordsDay();
                    ky.DayID = int.Parse(dr["day_id"].ToString());
                    ky.KeyName = dr["key_name"].ToString();
                    ky.WebUrl = dr["web_url"].ToString();
                    ky.CachedDate = dr["cached_date"].ToString();
                    list.Add(ky);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定KeywordsDay -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往KeywordsDay表中插入数据
        /// <param name="kr">实例化KeywordsDay类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertKeywordsDayDAL(SEO.Model.KeywordsDay kd)
        {
            int iNum = 0;
            string strSql = "insert into KeywordsDay(day_id,key_name,web_url,cached_date) " +
             " values(" + kd.DayID + ",'" + kd.KeyName + "','" + kd.WebUrl + "','" + kd.CachedDate + "')";
            try
            {
                iNum = SEO.DBSqlHelp.DBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("往KeywordsDay表中插入数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除KeywordsDay表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteKeywordsDayDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.DBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定KeywordsDay -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新KeywordsDay表中数据
        /// </summary>
        /// <param name="kr">实例化KeywordsDay类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateKeywordsDayDAL(SEO.Model.KeywordsDay kd)
        {
            int iNum = 0;
            //domain_regtime=datetime('" + kr.DomainRegTime + "', 'localtime')
            string strSql = "update KeywordsDay set key_name=" + kd.KeyName + ",web_url=" + kd.WebUrl + " where day_id=" + kd.DayID;
            try
            {
                iNum = SEO.DBSqlHelp.DBSQLiteHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新KeywordsDay表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表KeywordsDay到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindKeywordsDayDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "KeywordsDay");
                //ds.Tables["KeywordsDay"].Columns.Add("ViewDomainKeyword", typeof(string));
                for (int i = 0; i < ds.Tables["KeywordsDay"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["KeywordsDay"].Rows[i];
                }
                dgv.DataSource = ds.Tables["KeywordsDay"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定KeywordsDay -> " + ex.Message);
            }
        }

        /// <summary>
        /// 获取表中最大ID，并自增1
        /// </summary>
        /// <returns>返回表中最大ID，并自增1</returns>
        public int GetAutoAddNum()
        {
            int iNum = 0;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select day_id from KeywordsDay order by day_id desc limit 1";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    iNum = int.Parse(dr["day_id"].ToString()) + 1;
                }
                else
                {
                    iNum = 1;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定KeywordsDay -> " + ex.Message);
            }

            return iNum;
        }

        /// <summary>
        /// 检测是否已存在数据库中，屏蔽已抓取的数据
        /// </summary>
        /// <param name="strURL">链接地址</param>
        /// <returns>返回 True or False</returns>
        public bool CheckKeywordsDayIsExist(string strURL)
        {
            bool bValue = false;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select day_id from KeywordsDay where web_url='" + strURL + "'";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    bValue = true;
                }
                else
                {
                    bValue = false;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("检测是否已存在数据库中 -> " + ex.Message);
            }

            return bValue;
        }

    }
}
